//
//  EllipseView.m
//  yuikemall
//
//  Created by Gang.Wang on 12-6-26.
//  Copyright (c) 2012年 yuike. All rights reserved.
//

/*
 
 椭圆的定义：
 
 焦点在x轴时：
    椭圆的标准方程为x^2/a^2+y^2/b^2=1
 焦点在y轴时：
    椭圆的标准方程为y^2/a^2+x^2/b^2=1
 
 矩形内切椭圆
    长轴等于2a，短轴为2b
 
 */

#import "EllipseView.h"
#import "header.h"

@interface EllipseView() 
{
    CGContextRef mCtx;
    
    float ellipseA_;
    float ellipseB_;
    
    float ellipseC_;
    BOOL isEllipseFocusAtXAxis_;
    
    CGPoint focusPointA_;
    CGPoint focusPointB_;
    
    UIImage * imageContent_;
    
    
}

@property (nonatomic, assign) float ellipseA;
@property (nonatomic, assign) float ellipseB;

@property (nonatomic, assign) float ellipseC;
@property (nonatomic, assign) BOOL isEllipseFocusAtXAxis;

@property (nonatomic, assign) CGPoint focusPointA;
@property (nonatomic, assign) CGPoint focusPointB;

@property (nonatomic, strong) UIImage * imageContent;


- (void) privateInit;
- (void) drawEllipse;

- (void) calcEllipseParam;
@end



@implementation EllipseView

@synthesize ellipseA = ellipseA_;
@synthesize ellipseB = ellipseB_;

@synthesize ellipseC = ellipseC_;
@synthesize isEllipseFocusAtXAxis = isEllipseFocusAtXAxis_;

@synthesize focusPointA = focusPointA_;
@synthesize focusPointB = focusPointB_;

@synthesize imageContent = imageContent_;

- (void)dealloc
{
    YRelease(imageContent_)
    
    [super dealloc];
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        [self privateInit];
    }
    return self;
}



#pragma mark - Private Method
- (void) privateInit
{
    if(self.bInited)
    {
        return;
    }
    
    [self setBackgroundColor:COLOR_CLEAR];
        
    [self reAdjustLayout];
    mCtx = UIGraphicsGetCurrentContext();  
    
    
    self.bInited = TRUE;        
}

- (void) drawEllipse
{    
    if (!mCtx) 
    {
        mCtx = UIGraphicsGetCurrentContext();  
    }
    
    CGMutablePathRef ellipsePath = CGPathCreateMutable(); 

    
    CGRect ellipseFrame = CGRectMake(0, 0, self.width, self.height);
//    CGContextAddEllipseInRect(mCtx, ellipseFrame);
 
    CGPathAddEllipseInRect(ellipsePath, nil, ellipseFrame);
    
     UIBezierPath* path = [UIBezierPath bezierPath]; 
    path.CGPath = ellipsePath;
    
    [path addClip];
     CGContextAddPath(mCtx, path.CGPath);
//    CGContextSetFillColor(mCtx, CGColorGetComponents(COLOR_MAGENTA.CGColor));
//    CGContextFillPath(mCtx);
    
    CGContextClip(mCtx);
//    [self.imageContent drawAtPoint:CGPointZero];
    [self.imageContent drawInRect:self.bounds];
//    CGContextRestoreGState(mCtx);  
    CGPathRelease(ellipsePath); 
}


- (void) calcEllipseParam
{
    CGSize size = self.bounds.size;
    
    self.isEllipseFocusAtXAxis = TRUE;
    if (size.width < size.height) 
    {
        self.isEllipseFocusAtXAxis = FALSE;
    }
    
    
    if (self.isEllipseFocusAtXAxis) 
    {
        self.ellipseA = self.width / 2;
        self.ellipseB = self.height / 2;
    }
    else 
    {
        self.ellipseA = self.height / 2;
        self.ellipseB = self.width / 2;
    }

    self.ellipseC = sqrtf((self.ellipseA * self.ellipseA) - (self.ellipseB * self.ellipseB));

    if (self.isEllipseFocusAtXAxis) 
    {
        self.focusPointA = CGPointMake(self.boundsCenter.x - self.ellipseC,
                                       self.boundsCenter.y);  
        self.focusPointB = CGPointMake(self.boundsCenter.x + self.ellipseC, 
                                       self.boundsCenter.y);
    }
    else 
    {
        self.focusPointA = CGPointMake(self.boundsCenter.y - self.ellipseC,
                                       self.boundsCenter.x);  
        self.focusPointB = CGPointMake(self.boundsCenter.y + self.ellipseC, 
                                   self.boundsCenter.x);
    }
    

}

#pragma mark - Publish Method

- (void) setFrame:(CGRect)frame
{
    [super setFrame:frame];
    
    [self calcEllipseParam];
}

- (void) setImage:(UIImage *) imageValue
{
    [super setImage:imageValue];
 
    self.imageContent = imageValue;
    [self setNeedsDisplay];
}
- (void) reAdjustLayout
{
    [self setNeedsDisplay];
}

- (BOOL) isVisibleAtPoint:(CGPoint)point
{
    BOOL result = TRUE;
    
    result = [YMath pointIsInEllipse:point
                            ellipseA:self.ellipseA
                            ellipseB:self.ellipseB 
                 isFocusPointAtXAxis:self.isEllipseFocusAtXAxis 
                         centerPoint:self.boundsCenter];
    
    DebugLog(@" is clicked in shape: %d", result );
    return result;
}

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
    CGContextSaveGState(mCtx);
    [self drawEllipse];
}


@end
